//
//  InfoListRowsGroupsCell.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/23.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

private let itemHeight: CGFloat = 35

class InfoListRowsCell: UITableViewCell {

    var itemViews: [InfoListRowItemView] = []

    let firstTexts = ["序号", "订单号", "客户单号", "产品", "客户姓名", "面料", "交货日期", "数量", "总价"]

    var items: [InfoListRowItemable] = [] {
        didSet {
           if items.count == oldValue.count { return }
            while itemViews.count < items.count + 1{
                let itemView = InfoListRowItemView()
                contentView.addSubview(itemView)
                itemViews.append(itemView)
            }

            while itemViews.count > items.count + 1{
                itemViews.last?.removeFromSuperview()
                _ = itemViews.removeLast()
            }

            for (index, itemView) in itemViews.enumerated() {
                if index == 0 {
                    itemView.texts = firstTexts
                } else {
                    itemView.texts = items[index - 1].titles
                }
                let width = superview?.frame.width ?? 0
                itemView.frame = CGRect(x: 0, y: CGFloat(index) * itemHeight, width: width, height: itemHeight)
            }

            self.contentView.mas_updateConstraints { (make) in
                make?.height.equalTo()(itemViews.last!.frame.maxY)
            }
        }
    }
}

